﻿#include "FITKAbaqusHDF5AdaptorTimePoints.h"
#include "FITK_Kernel/FITKCore/FITKEnumTransformer.hpp"
#include "FITK_Interface/FITKInterfaceIO/FITKAbstractHDF5Reader.h"
#include "FITK_Interface/FITKInterfaceIO/FITKAbstractHDF5Writer.h"
#include "FITK_Interface/FITKInterfaceModel/FITKCoordinateSystem.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaTransform.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaTimePoints.h"

namespace IO
{
    QString FITKAbaqusHDF5AdaptorTimePoints::getAdaptorClass()
    {
        return "FITKAbaqusHDF5AdaptorTimePoints";
    }

    bool FITKAbaqusHDF5AdaptorTimePoints::adaptR()
    {
        auto tp = dynamic_cast<Interface::FITKAbaTimePoints*>(_dataObj);
        if (!_reader || !tp || !_h5Group) return false;
        bool isR = true;
        isR &= readTimePoints(tp, *_h5Group);
        return isR;
    }

    bool FITKAbaqusHDF5AdaptorTimePoints::adaptW()
    {
        auto tp = dynamic_cast<Interface::FITKAbaTimePoints*>(_dataObj);
        if (!_writer || !tp || !_h5Group) return false;
        bool isW = true;
        isW &= writeTimePoints(tp, *_h5Group);
        return isW;
    }

    bool FITKAbaqusHDF5AdaptorTimePoints::readTimePoints(Interface::FITKAbaTimePoints* tp, H5::Group& h5Group)
    {
        if (!tp) return false;
        bool isR = readNDataObject(tp, h5Group);
        bool specifyUsingDelimiters = readBoolAttribute(h5Group, "SpecifyUsingDelimiters");
        tp->setSpecifyUsingDelimiters(specifyUsingDelimiters);
        int numberOfRows = readIntAttribute(h5Group, "NumberOfRows");
        tp->setNumberOfRows(numberOfRows);
        for (int i = 0; i < numberOfRows; ++i)
        {
            QString indexName = QString::number(i);
            auto NumGroup = h5Group.openGroup(indexName.toStdString());
            double point = readDoubleAttribute(NumGroup, "Point");
            tp->setPoint(point, i);
            double start = readDoubleAttribute(NumGroup, "Start");
            tp->setStart(start, i);
            double end = readDoubleAttribute(NumGroup, "End");
            tp->setEnd(end, i);
            double increment = readDoubleAttribute(NumGroup, "Increment");
            tp->setIncrement(increment, i);
        }
        return isR;
    }

    bool FITKAbaqusHDF5AdaptorTimePoints::writeTimePoints(Interface::FITKAbaTimePoints* tp, H5::Group& h5Group)
    {
        if (!tp) return false;

        std::string groupName = createParentAttribute(tp, h5Group);
        if (groupName.empty())return false;

        auto tpGroup = h5Group.createGroup(groupName);

        bool specifyUsingDelimiters = tp->getSpecifyUsingDelimiters();
        writeBoolAttribute(tpGroup, "SpecifyUsingDelimiters", &specifyUsingDelimiters);
        int numberOfRows = tp->getNumberOfRows();
        writeIntAttribute(tpGroup, "NumberOfRows", &numberOfRows);
        for (int i = 0; i < numberOfRows; ++i)
        {
            QString indexName = QString::number(i);
            auto NumGroup = tpGroup.createGroup(indexName.toStdString());
            double point = tp->getPoint(i);
            writeDoubleAttribute(NumGroup, "Point", &point);
            double start = tp->getStart(i);
            writeDoubleAttribute(NumGroup, "Start", &start);
            double end = tp->getEnd(i);
            writeDoubleAttribute(NumGroup, "End", &end);
            double increment = tp->getIncrement(i);
            writeDoubleAttribute(NumGroup, "Increment", &increment);
        }

        return writeNDataObject(tp, tpGroup);
    }
}


